function deepCopy(obj) {
    if (typeof obj !== 'object') {
        return obj;
    }
    let newobj = {};
    for (let attr in obj) {
        if (attr !== null) {
            newobj[attr] = deepCopy(obj[attr]);
        }
    }
    return newobj;
}

function groupArray(list, attr) {
    let map = {},
        dest = [];
    for (let i = 0; i < list.length; i++) {
        let ai = list[i];
        if (!map[ai[attr]]) {
            dest.push({
                initial: ai[attr],
                list: [ai]
            });
            map[ai[attr]] = ai;
        } else {
            for (let j = 0; j < dest.length; j++) {
                let dj = dest[j];
                if (dj.initial === ai[attr]) {
                    dj.list.push(ai);
                    break;
                }
            }
        }
    }
    return dest;
}

const createRandomId = () => {
    return (Math.random() * 10000000).toString(16).substr(0, 4) + '-' + (new Date()).getTime() + '-' + Math.random().toString().substr(2, 5);
}

/**
 * 读取文件刘转化为fileStr
 * @param rawFile
 * @returns {Promise<any>}
 */
function loadFile(rawFile) {
    return new Promise(function (resolve, reject) {
        let reader = new FileReader();
        //读取文件过程方法
        reader.onloadstart = function (e) {
            console.log("开始读取....");
        };
        reader.onprogress = function (e) {
            console.log("正在读取中....");
        };
        reader.onabort = function (e) {
            console.log("中断读取....");
            reject(e.target.error);
        };
        reader.onerror = function (e) {
            console.log("读取异常....");
            reject(e.target.error);
        };
        reader.onload = function (e) {
            console.log("成功读取....");
            resolve(e.target.result);
        };
        reader.readAsBinaryString(rawFile);
    });
}

export default {
    copyObject: deepCopy,
    groupArray: groupArray,
    loadFile: loadFile,
    createRandomId
}
